package com.music.config;

/**
 * @author 名一
 * @ClassName Alipay
 * @description: TODO
 * @datetime 2022年 07月 11日 17:10
 * @version: 1.0
 */

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.music.entity.AlipayBean;
import org.springframework.stereotype.Component;

/**
 * 支付宝接口
 */
@Component
public class Alipay {
    /**
     * 支付接口
     * @param alipayBean
     * @return
     * @throws AlipayApiException
     */
    public String pay(AlipayBean alipayBean) throws AlipayApiException {
        // 1、获得初始化的AlipayClient
        /*String serverUrl = AlipayProperties.getGatewayUrl();
        String appId = AlipayProperties.getAppId();
        String privateKey = AlipayProperties.getPrivateKey();
        String format = "json";
        String charset = AlipayProperties.getCharset();
        String alipayPublicKey = AlipayProperties.getPublicKey();
        String signType = AlipayProperties.getSignType();
        String returnUrl = AlipayProperties.getReturnUrl();
        String notifyUrl = AlipayProperties.getNotifyUrl();*/
        String serverUrl = "https://openapi.alipaydev.com/gateway.do";
        String appId = "2021000120609054";
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCwYG+RPB08BRnRJ6F1EoxX0ubc7T6FC7lk0OSZ0P2mnZ6gqHZygeolKNjCHaL6E6/rw/drBMfrcEu8jZR7vYDtkOJmzwpl7MRz6JNLPB9x2d1WTys/QSi8omLzCDnTDJEpzqliOXdV2yRnBr/plWPT3sqMkLowZR9sGQmrhJcs+dciQSu26/STgwg0c1NfVZcop66nFRUk8n9F6SEjTtUS7Wch9J7dRKkuSnUTOq4kOhv9rTNNDOSgnWoUwNKmDtQ8rQ0FhsXgQ36+i4GxdbFTxn/rQy43vSOUkBEKHtzrlhdCGF6hEJ/MF+tFJ+nJuNR1C5y6LImoxQk1kihPIV/jAgMBAAECggEBAIf/9EdC2S/8pumV63OAd+MIZqeX8Ata/Pt176+DbPLauwQaoiqtbkR0+J7Fh3PZV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zRfTtot4DeeNDQnrh8YhQeT+4XtnkDnuHtag0r8TYJQWVdxVcfVxoGyk2Ut75XDZrojj2u3Q/flAH/DDQmGHw==";
        String format = "json";
        String charset = "utf-8";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjQUTI1ZOy22u8teASTwPy4CFwHmjec55ayWOXvh9YJC9RcVzAUhArdm7lfDWXHkdHEdftOH0C76FRupRs6Nqe2GKjjI7b7lWa+b3oqrPa8v+DFhP0+UQNhY8J9+yv1GPYOPtt0vDtQtidIzbQDZMBMHcRK+YMUAohILoB0GV2rOoJ9AkWw4c0D6kcdxUnAYn+9wfwJCa5gfciPA2BK0JVSfpWnWQqCzcpH0KsW0hDAr9MuihpWnh5Osj21/2BdHXg96MKGqNZTAAItdB+bHP5ISEf8hS5U2TFiVmQw9hyUshGGBhGs4pqCnbXSowPjRB/Tjr87e5qkAc6OOp3L+bhQIDAQAB";
        String signType = "RSA2";
        String returnUrl = "http://127.0.0.1:8080/games_shopping/front/login.html";
        String notifyUrl = "http://127.0.0.1:8080/games_shopping/front/login.html";
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        // 2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(returnUrl);
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(notifyUrl);
        // 封装参数
        /*alipayRequest.setBizContent("{\"out_trade_no\":\""+ alipayBean.getOut_trade_no() +"\","
                + "\"total_amount\":\""+ alipayBean.getTotal_amount() +"\","
                + "\"subject\":\""+ alipayBean.getSubject() +"\","
                + "\"body\":\""+ alipayBean.getBody() +"\","
                + "\"timeout_express\":\"30m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");*/
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // 返回付款信息
        return result;
    }
}
